package com.lzj.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzj.admin.mapper.CustomerMapper;
import com.lzj.admin.mapper.TownMapper;
import com.lzj.admin.pojo.*;
import com.lzj.admin.mapper.AreaMapper;
import com.lzj.admin.query.AreaQuery;
import com.lzj.admin.service.IAreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzj.admin.service.ITownService;
import com.lzj.admin.utils.AssertUtil;
import com.lzj.admin.utils.PageResultUtil;
import com.lzj.admin.utils.PinYinUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 村信息表 服务实现类
 * </p>
 *
 * @author luoyoumou
 * @date 2022-12-08
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService {

    @Resource
    CustomerMapper customerMapper;

    @Resource
    ITownService townService;

    @Resource
    TownMapper townMapper;


    @Override
    public Map<String, Object> areaList(AreaQuery areaQuery) {
        IPage<Area> page = new Page<Area>(areaQuery.getPage(),areaQuery.getLimit());
        page = this.baseMapper.queryAreaByParams(page,areaQuery);
        return PageResultUtil.getResult(page.getTotal(),page.getRecords());
    }

    /**
     * 在指定乡镇中：查看是否存在 指定名称 的村
     * @param name
     * @param townId
     * @return
     */
    private Area findAreaByNameTownId(String name,Integer townId){
        return this.getOne(new QueryWrapper<Area>().eq("is_del",0).eq("name",name).eq("town_id",townId));
    }

    /**
     * 排除指定村Id & 乡镇Id, 查看是否存在 指定名称 的村
     * @param name
     * @param townId
     * @param id
     * @return
     */
    private Area findAreaByNameTownId(String name, Integer townId, Integer id) {
        return this.getOne(new QueryWrapper<Area>().eq("name",name.trim()).eq("town_id",townId).notIn("id",id));
    }


    /**
     * 添加村
     *  村名 非空
     *  村名 + 所属乡镇 不能重复
     * @param area
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void saveArea(Area area) {
        AssertUtil.isTrue(StringUtils.isBlank(area.getName().trim()),"村名不能为空！");
        AssertUtil.isTrue(null == area.getTownId(),"请指定村所属乡镇！");
        QueryWrapper<Town> townQueryWrapper = new QueryWrapper<Town>().eq("is_del",0).eq("id",area.getTownId()).last("limit 1");
        List<Town> tTown = townMapper.selectList(townQueryWrapper);
        AssertUtil.isTrue(tTown.isEmpty(),"指定乡镇不存在！");
        AssertUtil.isTrue(null != this.findAreaByNameTownId(area.getName().trim(),area.getTownId()),"在该乡镇中，该村名已存在！");
        area.setName(area.getName().trim());
        area.setIsDel(0);
        area.setFpyName(PinYinUtil.getFirstSpell(area.getName()));
        AssertUtil.isTrue(!(this.save(area)),"村信息添加失败！");
    }

    /**
     * 更新村
     *  村名 非空
     *  村名 + 所属乡镇 不能重复
     * @param area
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateArea(Area area) {
        AssertUtil.isTrue(null == this.getById(area.getId()),"请选择村记录！");
        AssertUtil.isTrue(null != this.findAreaByNameTownId(area.getName().trim(),area.getTownId(),area.getId()),"在该乡镇中，该村名已存在！");
        area.setName(area.getName().trim());
        area.setFpyName(PinYinUtil.getFirstSpell(area.getName()));
        AssertUtil.isTrue(!(this.updateById(area)),"村信息更新失败！");
    }

    /**
     * 删除村
     *   该村下不能有相关客户记录
     * @param id
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deleteArea(Integer id) {
        Area tempArea = this.getById(id);
        AssertUtil.isTrue(null == tempArea,"待删除的村记录不存在！");
        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<Customer>().eq("area_id",id).last("limit 1");
        List<Customer> customer = customerMapper.selectList(customerQueryWrapper);
        AssertUtil.isTrue(!(customer.isEmpty()),"该村已存在客户信息，不能被删除！");
        tempArea.setIsDel(1);
        AssertUtil.isTrue(!(this.updateById(tempArea)),"村信息删除失败！");
    }
}
